﻿
//***********************************************************************
// * Module:   MsjBaBafwqyslHandler
// * 作者:  曹荣芝
// * Modified: 2013/5/30
// * Purpose: Definition of the Class MsjBaBafwqyslHandler 业务逻辑处理Handler类
// ***********************************************************************/

using System;
using System.Collections.Generic;
using System.Linq;
using System.Web;
using XWG.Framework.MSException;
using XWG.Framework.AuthUtility;
using XWG.Framework.AuthEntity;
using XWG.Framework.AuthSPConst;
using XWG.Framework.AuthBLL;
using XWG.JWBST.JWBSTEntity;
using XWG.JWBST.JWBSTSPConst;
using System.Web.Script.Serialization;
using System.Data;
using XWG.Framework.Query;
using Newtonsoft.Json;
using Newtonsoft.Json.Converters;
using XWG.JWBST.JWBSTBLL;
using XWG.Framework;
namespace JWBSTAPP.AjaxServer
{
    /// <summary>
    /// MsjBaBafwqyslHandler 的摘要说明
    /// </summary>
    public class MsjBaBafwqyslHandler : JWBSTAPP.CommonClass.HttpHandlerBase, System.Web.SessionState.IRequiresSessionState
    {

        #region 添加记录
        /// <summary>
        /// 添加记录
        /// </summary>
        protected void AddRecord()
        {
            HttpContext context = HttpContext.Current;
            context.Response.ContentType = "text/plain";
            ResultModel mResultModel = new ResultModel(); //初始化返回类型
            try
            {
                var mUser = AppOrganizatiOnUserBLL.Load(context.Session["UserID"].ToString());
                MsjBaBafwqysl mMsjBaBafwqysl = new MsjBaBafwqysl();
                mMsjBaBafwqysl.Qyid = Guid.NewGuid().ToString(); //主键
                mMsjBaBafwqysl.Qymc = context.Request.Form["texQymc"]; //企业名称
                mMsjBaBafwqysl.Zcd = context.Request.Form["texZcd"]; //注册地(市县区)数据字典
                mMsjBaBafwqysl.Zsxz = context.Request.Form["texZsxz"]; //住所详址
                mMsjBaBafwqysl.Yb = context.Request.Form["texYb"]; //邮编
                mMsjBaBafwqysl.Fwfw = context.Request.Form["texFwfw"]; //服务范围(字典)
                mMsjBaBafwqysl.Lxrxm = context.Request.Form["texLxrxm"]; //联系人姓名
                mMsjBaBafwqysl.Gddh = context.Request.Form["texGddh"]; //固定电话
                mMsjBaBafwqysl.Sj = context.Request.Form["texSj"]; //手机
                mMsjBaBafwqysl.Jjlx = context.Request.Form["texJjlx"]; //经济类型
                mMsjBaBafwqysl.Zczb = context.Request.Form["texZczb"]; //注册资本
                mMsjBaBafwqysl.Gdsl = context.Request.Form["texGdsl"]; //股东数量
                mMsjBaBafwqysl.Frxm = context.Request.Form["texFrxm"]; //法人姓名
                mMsjBaBafwqysl.Frxb = context.Request.Form["texFrxb"]; //法人性别(字典)
                mMsjBaBafwqysl.Frgszw = context.Request.Form["texFrgszw"]; //法人公司职务
                mMsjBaBafwqysl.Frzjmc = context.Request.Form["texFrzjmc"]; //法人证件名称
                mMsjBaBafwqysl.Frzjhm = context.Request.Form["texFrzjhm"]; //法人证件号码
                mMsjBaBafwqysl.Frzz = context.Request.Form["texFrzz"]; //法人住址
                mMsjBaBafwqysl.Frbaszbh = context.Request.Form["texFrbaszbh"]; //法人保安师证编号
                mMsjBaBafwqysl.Frgddh = context.Request.Form["texFrgddh"]; //法人固定电话
                mMsjBaBafwqysl.Frsj = context.Request.Form["texFrsj"]; //法人手机
                mMsjBaBafwqysl.Glryxm = context.Request.Form["texGlryxm"]; //管理人一姓名
                mMsjBaBafwqysl.Glryzw = context.Request.Form["texGlryzw"]; //管理人一职务
                mMsjBaBafwqysl.Glryzjmc = context.Request.Form["texGlryzjmc"]; //管理人一证件名称
                mMsjBaBafwqysl.Glryzjhm = context.Request.Form["texGlryzjhm"]; //管理人一证件号码
                mMsjBaBafwqysl.Glrybasbh = context.Request.Form["texGlrybasbh"]; //管理人一保安师证编号
                mMsjBaBafwqysl.Glrexm = context.Request.Form["texGlrexm"]; //管理人二姓名
                mMsjBaBafwqysl.Glrezw = context.Request.Form["texGlrezw"]; //管理人二职务
                mMsjBaBafwqysl.Glrezjmc = context.Request.Form["texGlrezjmc"]; //管理人二证件名称
                mMsjBaBafwqysl.Glrezjhm = context.Request.Form["texGlrezjhm"]; //管理人二证件号码
                mMsjBaBafwqysl.Glrebasbh = context.Request.Form["texGlrebasbh"]; //管理人二保安师证编号
                mMsjBaBafwqysl.Glrsxm = context.Request.Form["texGlrsxm"]; //管理人三姓名
                mMsjBaBafwqysl.Glrszw = context.Request.Form["texGlrszw"]; //管理人三职务
                mMsjBaBafwqysl.Glrszjmc = context.Request.Form["texGlrszjmc"]; //管理人三证件名称
                mMsjBaBafwqysl.Glrszjhm = context.Request.Form["texGlrszjhm"]; //管理人三证件号码
                mMsjBaBafwqysl.Glrsbasbh = context.Request.Form["texGlrsbasbh"]; //管理人三保安师证编号
                //mMsjBaBafwqysl.Frqm = context.Request.Form["texFrqm"]; //法人签名
                //mMsjBaBafwqysl.Qmrq = context.Request.Form["texQmrq"]; //签名日期
                mMsjBaBafwqysl.Cjsj = DateTime.Now.ToString(); ; //创建时间(2013-05-28 09:13:01)
                mMsjBaBafwqysl.Cjr = mUser.UserId; //创建人(人员ID)
                mMsjBaBafwqysl.Cjrdw = mUser.OrgnaId; //创建单位(单位ID)
                //mMsjBaBafwqysl.Xgr = context.Request.Form["texXgr"]; //修改人(人员ID)
                //mMsjBaBafwqysl.Xgrdw = context.Request.Form["texXgrdw"]; //修改人单位(单位ID)
                //mMsjBaBafwqysl.Xgsj = context.Request.Form["texXgsj"]; //修改时间(2013-05-28 09:13:01)
                mMsjBaBafwqysl.Enabled = "1"; //是否有效（1有效，0无效）
                mMsjBaBafwqysl.Movesign = "0"; //是否传输（1已，0未）

                mResultModel = MsjBaBafwqyslBLL.Insert(mMsjBaBafwqysl, context.Session["UserID"], true); //新建操作
                context.Response.Write(JsonConvert.SerializeObject(mResultModel)); //序列化后返回
            }
            catch (Exception ex)
            {
                MSEx.DealErr(ex, "");
                mResultModel.ErrorCode = -9999;
                mResultModel.ISSucceed = false;
                context.Response.Write(JsonConvert.SerializeObject(mResultModel)); //序列化后返回
            }
        }
        #endregion

        #region 删除记录
        /// <summary>
        /// 删除记录
        /// </summary>
        protected void DeleteRecord()
        {

            HttpContext context = HttpContext.Current;
            context.Response.ContentType = "text/plain";
            ResultModel mResultModel = new ResultModel(); //声明返回类型
            try
            {
                string[] arryParamKey = context.Request.Params["id"].ToString().Split(',');
                if (arryParamKey.Length == 0)
                {
                    mResultModel.ErrorCode = -9991;
                    mResultModel.ISSucceed = false;
                    context.Response.Write(JsonConvert.SerializeObject(mResultModel)); //序列化后返回    
                    return;
                }
                MsjBaBafwqysl mMsjBaBafwqysl = new MsjBaBafwqysl();
                foreach (var paramKey in arryParamKey)
                {
                    mResultModel = MsjBaBafwqyslBLL.Delete(paramKey, context.Session["UserID"], true); //删除对应的信息
                }
                context.Response.Write(JsonConvert.SerializeObject(mResultModel)); ; //序列化后返回
            }
            catch (Exception ex)
            {
                MSEx.DealErr(ex, ""); //写入异常日志
                mResultModel.ErrorCode = -9999; //-9999代码是系统异常通用代码
                mResultModel.ISSucceed = false; //行为执行失败
                context.Response.Write(JsonConvert.SerializeObject(mResultModel)); //序列化后返回
            }

        }
        #endregion
        #region 修改记录
        /// <summary>
        /// 修改记录
        /// </summary>
        protected void EditRecord()
        {
            HttpContext context = HttpContext.Current;
            context.Response.ContentType = "text/plain";
            ResultModel mResultModel = new ResultModel(); //初始化返回类型
            MsjBaBafwqysl mMsjBaBafwqysl = new MsjBaBafwqysl();

            string paramKey = context.Request.Params["id"];
            try
            {

                if (string.IsNullOrEmpty(paramKey))
                {
                    mResultModel.ErrorCode = -9991;
                    mResultModel.ISSucceed = false;
                    context.Response.Write(JsonConvert.SerializeObject(mResultModel)); //序列化后返回    
                    return;
                }
                mMsjBaBafwqysl = MsjBaBafwqyslBLL.Load(paramKey); //根据键值获取实体信息
                if (mMsjBaBafwqysl != null)
                {
                    var mUser = AppOrganizatiOnUserBLL.Load(context.Session["UserID"].ToString());
                    mMsjBaBafwqysl.Qymc = context.Request.Form["texQymc"]; //企业名称
                    mMsjBaBafwqysl.Zcd = context.Request.Form["texZcd"]; //注册地(市县区)数据字典
                    mMsjBaBafwqysl.Zsxz = context.Request.Form["texZsxz"]; //住所详址
                    mMsjBaBafwqysl.Yb = context.Request.Form["texYb"]; //邮编
                    mMsjBaBafwqysl.Fwfw = context.Request.Form["texFwfw"]; //服务范围(字典)
                    mMsjBaBafwqysl.Lxrxm = context.Request.Form["texLxrxm"]; //联系人姓名
                    mMsjBaBafwqysl.Gddh = context.Request.Form["texGddh"]; //固定电话
                    mMsjBaBafwqysl.Sj = context.Request.Form["texSj"]; //手机
                    mMsjBaBafwqysl.Jjlx = context.Request.Form["texJjlx"]; //经济类型
                    mMsjBaBafwqysl.Zczb = context.Request.Form["texZczb"]; //注册资本
                    mMsjBaBafwqysl.Gdsl = context.Request.Form["texGdsl"]; //股东数量
                    mMsjBaBafwqysl.Frxm = context.Request.Form["texFrxm"]; //法人姓名
                    mMsjBaBafwqysl.Frxb = context.Request.Form["texFrxb"]; //法人性别(字典)
                    mMsjBaBafwqysl.Frgszw = context.Request.Form["texFrgszw"]; //法人公司职务
                    mMsjBaBafwqysl.Frzjmc = context.Request.Form["texFrzjmc"]; //法人证件名称
                    mMsjBaBafwqysl.Frzjhm = context.Request.Form["texFrzjhm"]; //法人证件号码
                    mMsjBaBafwqysl.Frzz = context.Request.Form["texFrzz"]; //法人住址
                    mMsjBaBafwqysl.Frbaszbh = context.Request.Form["texFrbaszbh"]; //法人保安师证编号
                    mMsjBaBafwqysl.Frgddh = context.Request.Form["texFrgddh"]; //法人固定电话
                    mMsjBaBafwqysl.Frsj = context.Request.Form["texFrsj"]; //法人手机
                    mMsjBaBafwqysl.Glryxm = context.Request.Form["texGlryxm"]; //管理人一姓名
                    mMsjBaBafwqysl.Glryzw = context.Request.Form["texGlryzw"]; //管理人一职务
                    mMsjBaBafwqysl.Glryzjmc = context.Request.Form["texGlryzjmc"]; //管理人一证件名称
                    mMsjBaBafwqysl.Glryzjhm = context.Request.Form["texGlryzjhm"]; //管理人一证件号码
                    mMsjBaBafwqysl.Glrybasbh = context.Request.Form["texGlrybasbh"]; //管理人一保安师证编号
                    mMsjBaBafwqysl.Glrexm = context.Request.Form["texGlrexm"]; //管理人二姓名
                    mMsjBaBafwqysl.Glrezw = context.Request.Form["texGlrezw"]; //管理人二职务
                    mMsjBaBafwqysl.Glrezjmc = context.Request.Form["texGlrezjmc"]; //管理人二证件名称
                    mMsjBaBafwqysl.Glrezjhm = context.Request.Form["texGlrezjhm"]; //管理人二证件号码
                    mMsjBaBafwqysl.Glrebasbh = context.Request.Form["texGlrebasbh"]; //管理人二保安师证编号
                    mMsjBaBafwqysl.Glrsxm = context.Request.Form["texGlrsxm"]; //管理人三姓名
                    mMsjBaBafwqysl.Glrszw = context.Request.Form["texGlrszw"]; //管理人三职务
                    mMsjBaBafwqysl.Glrszjmc = context.Request.Form["texGlrszjmc"]; //管理人三证件名称
                    mMsjBaBafwqysl.Glrszjhm = context.Request.Form["texGlrszjhm"]; //管理人三证件号码
                    mMsjBaBafwqysl.Glrsbasbh = context.Request.Form["texGlrsbasbh"]; //管理人三保安师证编号
                    //mMsjBaBafwqysl.Frqm = context.Request.Form["texFrqm"]; //法人签名
                    //mMsjBaBafwqysl.Qmrq = context.Request.Form["texQmrq"]; //签名日期
                    //mMsjBaBafwqysl.Cjsj = context.Request.Form["texCjsj"]; //创建时间(2013-05-28 09:13:01)
                    //mMsjBaBafwqysl.Cjr = context.Request.Form["texCjr"]; //创建人(人员ID)
                    //mMsjBaBafwqysl.Cjrdw = context.Request.Form["texCjrdw"]; //创建单位(单位ID)
                    mMsjBaBafwqysl.Xgr = mUser.UserId; //修改人(人员ID)
                    mMsjBaBafwqysl.Xgrdw = mUser.OrgnaId; //修改人单位(单位ID)
                    mMsjBaBafwqysl.Xgsj = DateTime.Now.ToString(); //修改时间(2013-05-28 09:13:01)
                    mMsjBaBafwqysl.Enabled = "1"; //是否有效（1有效，0无效）
                    //mMsjBaBafwqysl.Movesign = ""; //是否传输（1已，0未）

                    mResultModel = MsjBaBafwqyslBLL.Update(mMsjBaBafwqysl, context.Session["UserID"], true); //新建操作
                }
                else
                {
                    mResultModel.ErrorCode = -9990; //没有获取所需的信息，信息可能已被删除
                    mResultModel.ISSucceed = false;

                }
                context.Response.Write(JsonConvert.SerializeObject(mResultModel)); //序列化后返回
            }
            catch (Exception ex)
            {
                MSEx.DealErr(ex, "");
                mResultModel.ErrorCode = -9999;
                mResultModel.ISSucceed = false;
                context.Response.Write(JsonConvert.SerializeObject(mResultModel)); //序列化后返回
            }
        }
        #endregion

        #region 获取实体记录
        /// <summary>
        /// 获取实体记录
        /// </summary>
        protected void GetModel()
        {
            HttpContext context = HttpContext.Current;
            context.Response.ContentType = "text/plain";
            ResultModel mResultModel = new ResultModel(); //初始化返回类型
            string paramKey = context.Request.Params["id"];
            string json = "";
            try
            {
                MsjBaBafwqysl mMsjBaBafwqysl = new MsjBaBafwqysl(); //声明实体
                if (!string.IsNullOrEmpty(paramKey))
                {
                    mMsjBaBafwqysl = MsjBaBafwqyslBLL.Load(paramKey); //根据键值获取管理员
                    if (mMsjBaBafwqysl != null)
                    {
                        json = JsonConvert.SerializeObject(mMsjBaBafwqysl);
                    }
                }
            }
            catch (Exception ex)
            {
                MSEx.DealErr(ex, "");
                mResultModel.ErrorCode = -9990;
                mResultModel.ISSucceed = false;
                json = JsonConvert.SerializeObject(mResultModel);
            }
            context.Response.Write(json);
        }
        #endregion
        #region 获取数据
        /// <summary>
        /// 获取数据记录
        /// </summary>
        protected void QueryRecord()
        {
            TableMessage tm = new TableMessage();
            HearderMessage hm = new HearderMessage();
            List<HearderMessage> list = new List<HearderMessage>();
            HttpContext context = HttpContext.Current;
            context.Response.ContentType = "text/plain";
            string pageSize = context.Request.Form["pageSize"];
            string pageIndex = context.Request.Form["pageIndex"];
            if (String.IsNullOrEmpty(pageSize) || String.IsNullOrEmpty(pageIndex) || !AnyString.IsNumeric(pageSize) || !AnyString.IsNumeric(pageIndex))
                return;
            DataTable dtMsjBaBafwqysl = new DataTable();
            int intPageSize = Convert.ToInt32(pageSize);
            int intPageIndex = Convert.ToInt32(pageIndex);
            string name = context.Request.Form["texName"];
            string address = context.Request.Form["texAddress"];
            QueryByPage Q = new QueryByPage();
            QueryInfo qInfo = new QueryInfo();
            SqlConditionList sqllist = new SqlConditionList();
            sqllist.AddLogic(new SqlConditionList.SqlCondition(SqlCompareOper.EQUAL, MsjBaBafwqyslConst.Columns.Enabled, "1"), SqlLogicOper.AND_LOGIC);
            if (!string.IsNullOrEmpty(name))
            {
                sqllist.AddLogic(new SqlConditionList.SqlCondition(SqlCompareOper.LIKE, MsjBaBafwqyslConst.Columns.Qymc, '%' + name + '%'), SqlLogicOper.AND_LOGIC);
            }
            if (!string.IsNullOrEmpty(address))
            {
                sqllist.AddLogic(new SqlConditionList.SqlCondition(SqlCompareOper.LIKE, MsjBaBafwqyslConst.Columns.Zcd, '%' + address + '%'), SqlLogicOper.AND_LOGIC);
            }
            dtMsjBaBafwqysl = Q.GetRecordByPage("*", sqllist.ToSql(), "CJSJ", intPageSize, intPageIndex, MsjBaBafwqyslConst.TableName, MsjBaBafwqyslConst.FieldKey, "DESC").Tables[0];
            if (dtMsjBaBafwqysl != null && dtMsjBaBafwqysl.Rows.Count > 0)
            {
                for (int i = 0; i < dtMsjBaBafwqysl.Columns.Count; i++)
                {
                    hm = new HearderMessage();
                    hm.name = dtMsjBaBafwqysl.Columns[i].Caption;
                    list.Add(hm);
                }
                tm.Header = list;
                tm.data = dtMsjBaBafwqysl;
                tm.totalCount = Convert.ToString((int)Q.RowCount);
            }
            string json = JsonConvert.SerializeObject(tm);
            context.Response.Write(json);
        }
        #endregion

    }
}


